package com.wtf.generator.support;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wtf.generator.entity.WtfDataSource;
import com.wtf.generator.entity.WtfTemplateConfig;
import com.wtf.generator.model.dto.DsAddDTO;
import com.wtf.generator.service.WtfDataSourceService;
import com.wtf.generator.service.WtfTemplateConfigService;

import java.util.List;

/**
 * date: 2022/6/18 10:59
 * @author linhuanyou
 * @email 496558736@qq.com
 * @version 1.0
 */
public class PrimaryDsDbStoreSupport implements DsStoreSupport {

    private WtfDataSourceService wtfDataSourceService;

    private WtfTemplateConfigService wtfTemplateConfigService;

    public PrimaryDsDbStoreSupport(WtfDataSourceService wtfDataSourceService, WtfTemplateConfigService wtfTemplateConfigService) {
        this.wtfDataSourceService = wtfDataSourceService;
        this.wtfTemplateConfigService = wtfTemplateConfigService;
    }

    @Override
    public int addDataSource(DsAddDTO dsDbDTO) {
        WtfDataSource wtfDataSource = new WtfDataSource();
        wtfDataSource.setDs(dsDbDTO.getDs());
        wtfDataSource.setHost(dsDbDTO.getHost());
        wtfDataSource.setPort(dsDbDTO.getPort());
        wtfDataSource.setDbName(dsDbDTO.getDbName());
        wtfDataSource.setUsername(dsDbDTO.getUsername());
        wtfDataSource.setPassword(dsDbDTO.getPassword());
        wtfDataSource.setDbType(dsDbDTO.getDbType().getDb());
        boolean add = wtfDataSourceService.saveOrUpdate(wtfDataSource);
        return add ? 1 : 0;
    }

    @Override
    public int delDataSource(List<String> ds) {
        LambdaQueryWrapper<WtfTemplateConfig> wtfTemplateConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wtfTemplateConfigLambdaQueryWrapper.in(WtfTemplateConfig::getDs,ds);
        int desDsTc = wtfTemplateConfigService.getBaseMapper().delete(wtfTemplateConfigLambdaQueryWrapper);
        int delDs = wtfDataSourceService.getBaseMapper().deleteBatchIds(ds);
        return desDsTc + delDs;
    }
}
